#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

/*
 *  MessagePrint global variables
 */

    time_t now;
    struct tm *d;
    FILE * messageLog;

/*
 * Prints the message to the log file and to the standard output
 * with a timestamp on it
 *
 * <param=message> the message to print </param>
 */
void MessagePrint(const char * message)
{
    time(&now);
    d = localtime(&now);
    char logTime[20];
    strftime(logTime, 20, "%d/%m/%y %H:%M:%S", d);

    if(messageLog == NULL)
    {
        char logFileName[30];

        strftime(logFileName, 30, "serwerLog_%d_%m_%Y.log", d);

        messageLog = fopen(logFileName,"w");
    }


    fprintf(messageLog, "SERVER: %s: %s\n", logTime, message);
    printf("SERVER: %s: %s\n", logTime, message);

}

void MessageClose()
{
    if(messageLog != NULL)
    {
        fclose(messageLog);
    }
}

/*
 * Prints the message to the log file and to the standard output
 * Without any formating
 *
 * <param=message> the message to print </param>
 */
void MessagePrintClean(const char * message)
{
    if (messageLog == NULL) {
        time(&now);
        d = localtime(&now);
        char logFileName[30];

        strftime(logFileName, 30, "serwerLog_%d_%m_%Y.log", d);

        messageLog = fopen(logFileName, "w");
    }


    fprintf(messageLog, "%s", message);
    printf("%s", message);

}
